#ifndef WORLD_H
#define WORLD_H

#include <map>
#include <vector>

#include "Messages.h"
#include "DirectXStuff.h"
#include "CubeVolume.h"


#ifdef _DEBUG
	#include <assert.h>
	#define ASSERT(x) assert(x)
#else 
	#define ASSERT(x)
#endif

class Controller;
class Entity;

class World
{
public:
	friend class Network;

	~World(){};

	static World &Create()
	{
		if(g_instance == NULL)
		{
			g_instance = new World();
		}

		return *g_instance;
	}

	static void Delete()
	{
		delete g_instance;
		g_instance = NULL;
	}
	static World &Get()
	{
		ASSERT(g_instance != NULL);
		return *g_instance;
	}

	//int inline GetIntersection( float fDst1, float fDst2, D3DXVECTOR3 P1, D3DXVECTOR3 P2, D3DXVECTOR3 &Hit);
	//int inline InBox( D3DXVECTOR3 Hit, D3DXVECTOR3 B1, D3DXVECTOR3 B2, const int Axis);
	//bool CheckLineBox( D3DXVECTOR3 B1, D3DXVECTOR3 B2, D3DXVECTOR3 L1, D3DXVECTOR3 L2, D3DXVECTOR3 &Hit);

	//Functions for the game
	void CalculateShot(GameMessage _Msg);  //CHRIS::Don't pass in a message, interpret outside... maybe, if you want
	bool CalculateShot(D3DXVECTOR3 _Direction, float _Length, CubeVolume* _Cube);
	short CalculateShot(short Reference);

	//Helper Functions 
	void Update(float deltaT);
	void RenderObjects();
	void RenderPhysics(Entity* entity);
	void DrawHUD();
	std::map<short, Entity*> GetAllEntities()	{return m_entities;}
	//std::vector<Entity*> GetAllPlayers()	{return m_entities;}

	//CHRIS::I had to make this public, couldn't get friend classes working at all. Give it a shot if you want
	//Adding Objects/Entities
	void AddEntity(short reference, Entity *e);
	short AddEntityAutoRef(Entity *e);
	short GetNextReference(){ return m_currentEntityRef;}

	void AddController(Controller *c);
 
private:
	World(); //Singleton - Private Constructor
	World(const World &other);
	static World *g_instance;
 
	void SendClient();
	void SendServer();

	std::map<short, Entity*>		m_entities;
	std::vector<Controller*>	m_controllers;
	short m_currentEntityRef;
};

#include "Controller.h"
#include "Entity.h"

#endif